package controller

import (
	"fmt"
	"server/internal/dao"
	"server/internal/util"

	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	log "github.com/sirupsen/logrus"
)

func HandlerCreateIssueSubmission(c *gin.Context) {
	iid := c.Query("iid")
	sid := c.Query("sid")

	if iid == "" || sid == "" {
		logrus.Debug(iid, sid)
		logrus.Debug("issue id or submission id can not be empty")
		util.Errs(c, "issue id or submission id can not be empty")
		return
	}

	sbm, err := dao.SearchSubmissionByID(sid)
	if err != nil {
		logrus.Debug(err)
		util.Errs(c, "assign submission to issue failed")
		return
	}

	issue, err := dao.SearchIssueByID(iid)
	if err != nil {
		logrus.Debug(err)
		util.Errs(c, "assign submission to issue failed")
		return
	}

	if issue == nil || sbm == nil {
		log.Errorf("assign submission to issue failed, issue:%s or submission:%s not found", iid, sid)
		util.Errs(c, fmt.Sprintf("assign submission to issue failed, issue:%s or submission:%s not found", iid, sid))
		return
	}

	if err = dao.AssignSbmIssue(sbm, issue.ID); err != nil {
		log.Debug(err)
		util.Errs(c, "assign submission to issue failed")
		return
	}

	util.RespSuccessWithoutData(c, util.RequestSuccess)

}

func HandlerGetIssueSubmissions(c *gin.Context) {
	iid := c.Query("iid")

	if iid == "" {
		logrus.Debug(iid)
		logrus.Debug("issue id can not be empty")
		util.Errs(c, "issue id can not be empty")
		return
	}

	issue, err := dao.SearchIssueByID(iid)
	if err != nil {
		logrus.Debug(err)
		util.Errs(c, "get issue's submission failed")
		return
	}

	if issue == nil {
		log.Errorf("issue does not exist")
		util.Errs(c, "issue does not exist")
		return
	}

	sbms, err := dao.SearchSubmissionsByIssueID(iid)
	if err != nil {
		logrus.Debug(err)
		util.Errs(c, "get issue's submission failed")
		return
	}

	util.RespSuccess(c, sbms, util.RequestSuccess)
}

func HandlerDeleteIssueSubmission(c *gin.Context) {
	sid := c.Query("sid")

	if sid == "" {
		logrus.Debug(sid)
		logrus.Debug("submission id can not be empty")
		util.Errs(c, "submission id can not be empty")
		return
	}

	sbm, err := dao.SearchSubmissionByID(sid)
	if err != nil {
		logrus.Debug(err)
		util.Errs(c, "get submission by id failed")
		return
	}
	if sbm == nil {
		log.Errorf("submission does not exist")
		util.Errs(c, "submission does not exist")
		return
	}

	if err = dao.UnAssignSbmIssue(sbm); err != nil {
		log.Debug(err)
		util.Errs(c, "unassign submission to issue failed")
		return
	}

	util.RespSuccessWithoutData(c, util.RequestSuccess)
}

func HandlerUpdateIssueSubmission(c *gin.Context) {
	iid := c.Query("iid")
	sid := c.Query("sid")

	if iid == "" || sid == "" {
		logrus.Debug(iid, sid)
		logrus.Debug("issue id or submission id can not be empty")
		util.Errs(c, "issue id or submission id can not be empty")
		return
	}

	sbm, err := dao.SearchSubmissionByID(sid)
	if err != nil {
		logrus.Debug(err)
		util.Errs(c, "update submission to issue failed")
		return
	}

	issue, err := dao.SearchIssueByID(iid)
	if err != nil {
		logrus.Debug(err)
		util.Errs(c, "update submission to issue failed")
		return
	}

	if issue == nil || sbm == nil {
		log.Errorf("update submission to issue failed, issue:%s or submission:%s not found", iid, sid)
		util.Errs(c, fmt.Sprintf("update submission to issue failed, issue:%s or submission:%s not found", iid, sid))
		return
	}

	if err = dao.AssignSbmIssue(sbm, issue.ID); err != nil {
		log.Debug(err)
		util.Errs(c, "update submission to issue failed")
		return
	}

	util.RespSuccessWithoutData(c, util.RequestSuccess)
}
